# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ignacio Casal Quinteiro <icq@gnome.org>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=glib2
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=2.56.0.195.geceac66e8
provides=(${MINGW_PACKAGE_PREFIX}-${_realname}=$pkgver)
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname})
pkgrel=1
url="https://www.gtk.org/"
arch=('any')
pkgdesc="Common C routines used by GTK+ 2.4 and other libs (git) (mingw-w64)"
license=(LGPL2)
options=('debug' '!strip')
install=glib2-${CARCH}.install
depends=("${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-meson"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-docbook-xsl"
         "${MINGW_PACKAGE_PREFIX}-gtk-doc")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=("${_realname}::git+https://git.gnome.org/browse/glib"
        0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch
        0001-win32-Make-the-static-build-work-with-MinGW-when-pos.patch
        0028-inode_directory.patch
        missing-include.patch
        pyscript2exe.py
        )

sha256sums=('SKIP'
            'ef81e82e15fb3a71bad770be17fe4fea3f4d9cdee238d6caa39807eeea5da3e3'
            '838abaeab8ca4978770222ef5f88c4b464545dd591b2d532c698caa875b46931'
            'f7f06a90156fe0a308412512c359072922f7f0d19dd4bed30d863db18e48940b'
            '70dd5da0174896f52810b430d2aee17e0193da87444becb49f8494636f66369f'
            'f68b24932b3365c4098c04eeaeaf87275ceec29694b3f0597c431bbcf4f913a3'
            )

pkgver() {
  cd "${srcdir}"/${_realname}
  git describe --tags | sed 's|-|.|g'
}

prepare() {
  cd "${srcdir}"/${_realname}

  patch -Np1 -i "${srcdir}"/0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch
  patch -Np1 -i "${srcdir}"/0001-win32-Make-the-static-build-work-with-MinGW-when-pos.patch
  patch -Np1 -i "${srcdir}"/0028-inode_directory.patch
  patch -Np1 -i "${srcdir}"/missing-include.patch
}

build() {
  msg "Build static version"
  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static

  meson \
    --buildtype=plain \
    --default-library=static \
    -Dforce_posix_threads=true \
    ../${_realname}
  ninja

  msg "Build shared version"
  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  mkdir "${srcdir}"/build-${CARCH}-shared && cd "${srcdir}"/build-${CARCH}-shared

  meson \
    --buildtype=plain \
    -Ddefault_library=shared \
    -Dman=true \
    -Dforce_posix_threads=true \
    ../${_realname}
  ninja
}

package() {
  cd "${srcdir}/build-${CARCH}-static"
  DESTDIR="${pkgdir}${MINGW_PREFIX}" ninja install

  cd "${srcdir}/build-${CARCH}-shared"
  DESTDIR="${pkgdir}${MINGW_PREFIX}" ninja install

  install -Dm644 "${srcdir}/${_realname}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

  for name in glib-mkenums glib-genmarshal gdbus-codegen; do
    ${MINGW_PREFIX}/bin/python3 \
      "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/${name}"
  done

  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|$(cygpath -m ${MINGW_PREFIX})|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  sed -s "s|$(cygpath -m ${MINGW_PREFIX})|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/glib-gettextize"
}
